import { defineStore } from "pinia";
import { login as apiLogin } from "@/service/module/user";
import router from "@/router";
export const useUserStore = defineStore("user", {
  state: () => {
    return {
      token:null,
      user: {},
    };
  },
  getters: {
    getUser() {
      return this.user;
    },
  },
  persist: {
    enabled: true,
    strategies: [
      {
        key: "user",
        storage: localStorage,
        paths: ["user"],
      },
      {
        key: "token",
        storage: localStorage,
        paths: ["token"],
      },
    ],
  },
  actions: {
    async handleLogin(data) {
      const { token, userInfo} = await apiLogin(data);
      this.token = token;
      this.user = userInfo;
      router.push("/");
    },
    setUser(user) {
      this.user = user;
    },
    logout() {
      this.$reset();
      router.push("/login");
    },
  },
});